package com.supcalls.scheduled_task.config;

import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Objects;

/**
 * <p>分布式定时任务锁</p>
 * </p>
 * {@code @EnableSchedulerLock} PT固定格式 H小时 M是分钟 S是秒
 *
 * @author yueniao
 * @date 2020/09/21
 */
@Configuration
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "PT5M")
public class ShedLockConfig {

    @Bean
    public LockProvider lockProvider(RedisTemplate<String, Object> redisTemplate) {
        return new RedisLockProvider(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
    }
}
